<template>
  <div class="iframe-container">
    <!-- 加载状态指示器 -->
    <div class="loading-overlay" v-if="isLoading">
      <div class="loading-spinner"></div>
      <p>加载中...</p>
    </div>
    
    <!-- 错误提示 -->
    <div class="error-message" v-if="hasError">
      <p>{{ errorMessage }}</p>
      <button @click="retryLoading" class="retry-button">重试</button>
    </div>
    
    <!-- iframe内容 -->
    <iframe 
      v-show="!hasError"
      :src="url" 
      class="content-iframe" 
      frameborder="0" 
      allowfullscreen
      title="AI生成代码预览"
      @load="handleIframeLoad"
      @error="handleIframeError"
      ref="iframeRef"
    ></iframe>
  </div>
</template>

<script setup>
import { defineProps, ref, watch } from 'vue';

// 定义属性
const props = defineProps({
  url: {
    type: String,
    default: ''
  }
});

// 状态管理
const isLoading = ref(true);
const hasError = ref(false);
const errorMessage = ref('加载失败，请重试');
const iframeRef = ref(null);

// 处理iframe加载完成
const handleIframeLoad = () => {
  isLoading.value = false;
  hasError.value = false;
};

// 处理iframe加载错误
const handleIframeError = () => {
  isLoading.value = false;
  hasError.value = true;
  errorMessage.value = '内容加载失败，请重试';
};

// 重试加载
const retryLoading = () => {
  if (props.url) {
    isLoading.value = true;
    hasError.value = false;
    
    // 重新加载iframe
    if (iframeRef.value) {
      iframeRef.value.src = props.url;
    }
  }
};

// 监听URL变化
watch(() => props.url, (newUrl) => {
  if (newUrl) {
    isLoading.value = true;
    hasError.value = false;
  }
});
</script>

<style scoped>
.iframe-container {
  flex: 1;
  height: 100%;
  overflow: hidden;
  position: relative;
}

.content-iframe {
  width: 100%;
  height: 100%;
  border: none;
  background-color: #fff;
  box-shadow: 0 0 10px rgba(0, 0, 0, 0.05);
}

/* 加载状态样式 */
.loading-overlay {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  background-color: rgba(255, 255, 255, 0.9);
  z-index: 10;
  animation: fadeIn 0.3s ease;
}

.loading-spinner {
  width: 50px;
  height: 50px;
  border: 5px solid #f3f3f3;
  border-top: 5px solid var(--primary-color, #3498db);
  border-radius: 50%;
  animation: spin 1s linear infinite;
  margin-bottom: 15px;
}

@keyframes spin {
  0% { transform: rotate(0deg); }
  100% { transform: rotate(360deg); }
}

/* 错误提示样式 */
.error-message {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  background-color: #fff;
  padding: 20px;
  border-radius: 8px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
  text-align: center;
  max-width: 80%;
  z-index: 10;
}

.error-message p {
  margin-bottom: 15px;
  color: #e74c3c;
  font-weight: 500;
}

.retry-button {
  background-color: var(--primary-color, #3498db);
  color: white;
  border: none;
  padding: 8px 16px;
  border-radius: 4px;
  cursor: pointer;
  font-weight: 500;
  transition: background-color 0.2s ease;
}

.retry-button:hover {
  background-color: #2980b9;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .iframe-container {
    width: 100%;
  }
  
  .error-message {
    max-width: 90%;
    padding: 15px;
  }
}
</style>